package org.grayrabbit.cms.entity;

// Generated 2011-5-16 13:39:30 by Hibernate Tools 3.3.0.GA

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.util.HtmlTextConverter;
import org.grayrabbit.cms.util.Util;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;


/**
 * CmsContent generated by hbm2java
 */

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="Context_Type",
    discriminatorType=DiscriminatorType.STRING
)
@Table(name = "CMS_CONTENT")
public class Content implements java.io.Serializable {

	private static final long serialVersionUID = -3640626413237958778L;
	private String contentId;
	private Topic topic;
	private Category category;
	private String contentTitle;
	private String keywords;
	private String contentContext;
	private String contentDescription;
	private String contentAuthor;
	private Date createTime;
	private String createLoginName;
	private String createRealName;
	private String updateLoginName;
	private String updateRealName;
	private Date updateTime;
	private String auditLoginName;
	private String auditRealName;
	private Date auditTime;
	private String publishLoginName;
	private String publishRealName;
	private Date publishTime;
	private String contentStatus;
	private Integer contentHits;
	private String contentTpl;
	private Boolean isTop;
	private String contentSource;
	private String publishSug;
	private String showType;
	private Boolean isComment;
	private String simpleTitle1;
	private String simpleTitle2;
	private String simpleTitle3;
	private Integer publishType;
	private String temporaryUser;
	//二期
	private Boolean isLocked; //是否锁定
	private String siteId;//站点ID
	private Integer upCount;//评论顶数
	private String contentLabel;//文章分类：地域、行业、产品类型
	private Integer downCount;//评论踩数
	
	private Integer ord;
	
	//end
	@Column(name = "TEMPORARY_USER", length = 20)
	public String getTemporaryUser() {
		return temporaryUser;
	}

	public void setTemporaryUser(String temporaryUser) {
		this.temporaryUser = temporaryUser;
	}
	/**此变量用于 全站动态查询时，依据文章id跳转到生成的静态页面，使用此方法必须预先赋值*/
	private String visiturl;  // 临时变量
	
	@Column(name = "SIMPLE_TITLE_1", length = 256)
	public String getSimpleTitle1() {
		return simpleTitle1;
	}

	public void setSimpleTitle1(String simpleTitle1) {
		this.simpleTitle1 = simpleTitle1;
	}

	@Column(name = "SIMPLE_TITLE_2", length = 256)
	public String getSimpleTitle2() {
		return simpleTitle2;
	}

	public void setSimpleTitle2(String simpleTitle2) {
		this.simpleTitle2 = simpleTitle2;
	}

	@Column(name = "SIMPLE_TITLE_3", length = 256)
	public String getSimpleTitle3() {
		return simpleTitle3;
	}

	public void setSimpleTitle3(String simpleTitle3) {
		this.simpleTitle3 = simpleTitle3;
	}

	private Set<ContentAttach> contentAttachs = new HashSet<ContentAttach>(0);
	
	private Set<TopicContent> contentTopic = new HashSet<TopicContent>(0);//该文章被忽略的专题

	public Content() {
	}

	public Content(String contentId) {
		this.contentId = contentId;
	}

	@Id
	@Column(name = "CONTENT_ID", unique = true, nullable = false, length = 32)
	@GeneratedValue(generator = "paymentableGenerator")       
	@GenericGenerator(name = "paymentableGenerator", strategy = "uuid") 
	public String getContentId() {
		return this.contentId;
	}

	public void setContentId(String contentId) {
		this.contentId = contentId;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "TOPIC_ID")
	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "CATEGORY_ID")
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}
	
	@Column(name = "CONTENT_TITLE", length = 256)
	public String getContentTitle() {
		return this.contentTitle;
	}

	public void setContentTitle(String contentTitle) {
		this.contentTitle = contentTitle;
	}

	@Column(name = "KEYWORDS", length = 256)
	public String getKeywords() {
		return this.keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	@Column(name = "CONTENT_CONTEXT")
	@Type(type="org.springframework.orm.hibernate3.support.ClobStringType")
	public String getContentContext() {
		return this.contentContext;
	}

	public void setContentContext(String contentContext) {
		this.contentContext = contentContext;
	}

	@Column(name = "CONTENT_DESCRIPTION", length = 512)
	public String getContentDescription() {
		return this.contentDescription;
	}

	public void setContentDescription(String contentDescription) {
		this.contentDescription = contentDescription;
	}

	@Column(name = "CONTENT_AUTHOR", length = 32)
	public String getContentAuthor() {
		return this.contentAuthor;
	}

	public void setContentAuthor(String contentAuthor) {
		this.contentAuthor = contentAuthor;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "CREATE_TIME", length = 7)
	public Date getCreateTime() {
		return this.createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "CREATE_LOGIN_NAME", length = 64)
	public String getCreateLoginName() {
		return this.createLoginName;
	}

	public void setCreateLoginName(String createLoginName) {
		this.createLoginName = createLoginName;
	}

	@Column(name = "CREATE_REAL_NAME", length = 64)
	public String getCreateRealName() {
		return this.createRealName;
	}

	public void setCreateRealName(String createRealName) {
		this.createRealName = createRealName;
	}

	@Column(name = "UPDATE_LOGIN_NAME", length = 64)
	public String getUpdateLoginName() {
		return this.updateLoginName;
	}

	public void setUpdateLoginName(String updateLoginName) {
		this.updateLoginName = updateLoginName;
	}

	@Column(name = "UPDATE_REAL_NAME", length = 64)
	public String getUpdateRealName() {
		return this.updateRealName;
	}

	public void setUpdateRealName(String updateRealName) {
		this.updateRealName = updateRealName;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "UPDATE_TIME", length = 7)
	public Date getUpdateTime() {
		return this.updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	@Column(name = "AUDIT_LOGIN_NAME", length = 64)
	public String getAuditLoginName() {
		return this.auditLoginName;
	}

	public void setAuditLoginName(String auditLoginName) {
		this.auditLoginName = auditLoginName;
	}

	@Column(name = "AUDIT_REAL_NAME", length = 64)
	public String getAuditRealName() {
		return this.auditRealName;
	}

	public void setAuditRealName(String auditRealName) {
		this.auditRealName = auditRealName;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "AUDIT_TIME", length = 7)
	public Date getAuditTime() {
		return this.auditTime;
	}

	public void setAuditTime(Date auditTime) {
		this.auditTime = auditTime;
	}

	@Column(name = "PUBLISH_LOGIN_NAME", length = 64)
	public String getPublishLoginName() {
		return this.publishLoginName;
	}

	public void setPublishLoginName(String publishLoginName) {
		this.publishLoginName = publishLoginName;
	}

	@Column(name = "PUBLISH_REAL_NAME", length = 64)
	public String getPublishRealName() {
		return this.publishRealName;
	}

	public void setPublishRealName(String publishRealName) {
		this.publishRealName = publishRealName;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "PUBLISH_TIME", length = 7)
	public Date getPublishTime() {
		return this.publishTime;
	}

	public void setPublishTime(Date publishTime) {
		this.publishTime = publishTime;
	}

	@Column(name = "CONTENT_STATUS", length = 32)
	public String getContentStatus() {
		return this.contentStatus;
	}

	public void setContentStatus(String contentStatus) {
		this.contentStatus = contentStatus;
	}

	@Column(name = "CONTENT_HITS", precision = 6, scale = 0)
	public Integer getContentHits() {
		return this.contentHits;
	}

	public void setContentHits(Integer contentHits) {
		this.contentHits = contentHits;
	}

	@Column(name = "CONTENT_TPL", length = 64)
	public String getContentTpl() {
		return this.contentTpl;
	}

	public void setContentTpl(String contentTpl) {
		this.contentTpl = contentTpl;
	}

	@Column(name = "IS_TOP", precision = 1, scale = 0)
	public Boolean getIsTop() {
		return this.isTop;
	}

	public void setIsTop(Boolean isTop) {
		this.isTop = isTop;
	}

	@Column(name = "CONTENT_SOURCE", length = 2000)
	public String getContentSource() {
		return this.contentSource;
	}

	public void setContentSource(String contentSource) {
		this.contentSource = contentSource;
	}


	@OneToMany(fetch = FetchType.LAZY, mappedBy = "content")
	@org.hibernate.annotations.OrderBy(clause="focus_pic desc")
	public Set<ContentAttach> getContentAttachs() {
		return this.contentAttachs;
	}

	public void setContentAttachs(Set<ContentAttach> contentAttachs) {
		this.contentAttachs = contentAttachs;
	}
	@Column(name = "PUBLISH_SUG", length = 2000)
	public String getPublishSug() {
		return publishSug;
	}

	public void setPublishSug(String publishSug) {
		this.publishSug = publishSug;
	}
	@Column(name = "SHOW_TYPE", length = 2000)
	public String getShowType() {
		return showType;
	}

	public void setShowType(String showType) {
		this.showType = showType;
	}
	
	@Transient
	public String getContextType(){
		return "Content";
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "content")
	public Set<TopicContent> getContentTopic() {
		return contentTopic;
	}

	public void setContentTopic(Set<TopicContent> contentTopic) {
		this.contentTopic = contentTopic;
	}
	@Column(name = "IS_COMMENT", precision = 1, scale = 0)
	public Boolean getIsComment() {
		return isComment;
	}

	public void setIsComment(Boolean isComment) {
		this.isComment = isComment;
	}
	
	
	@Column(name = "IS_LOCKED", precision = 1, scale = 0)
	public Boolean getIsLocked() {
		return isLocked;
	}

	public void setIsLocked(Boolean isLocked) {
		this.isLocked = isLocked;
	}
	@Column(name = "SITE_ID", length = 32)
	public String getSiteId() {
		return siteId;
	}

	public void setSiteId(String siteId) {
		this.siteId = siteId;
	}

	@Column(name = "UP_COUNT", precision = 11, scale = 0)
	public Integer getUpCount() {
		return upCount;
	}

	public void setUpCount(Integer upCount) {
		this.upCount = upCount;
	}

	@Column(name = "CONTENT_LABEL", length = 32)
	public String getContentLabel() {
		return contentLabel;
	}

	public void setContentLabel(String contentLabel) {
		this.contentLabel = contentLabel;
	}

	@Column(name = "DOWN_COUNT", precision = 11, scale = 0)
	public Integer getDownCount() {
		return downCount;
	}

	public void setDownCount(Integer downCount) {
		this.downCount = downCount;
	}

	@Column(name = "ORD", precision = 6, scale = 0)
	public Integer getOrd() {
		return ord;
	}

	public void setOrd(Integer ord) {
		this.ord = ord;
	}

	@Transient
	public String cutTitle(int count){		 
		return Util.getMinString(contentTitle, simpleTitle1, simpleTitle2, simpleTitle3, count);
	}
	
	@Transient
	public  String getPublishUrl(){ 
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat sdf2 = new SimpleDateFormat("dd");
		if(publishTime==null) return "";
		String s = "content/"+sdf1.format(publishTime)
				+"/"+sdf2.format(publishTime)+"/"+contentId+".html";
		return s;
	}
	
	@Column(name = "PUBLISH_TYPE", precision = 6, scale = 0)
	public Integer getPublishType() {
		return publishType;
	}

	public void setPublishType(Integer publishType) {
		this.publishType = publishType;
	}
	
 
	/**附件中取小尺寸图片*/
	@Transient
	public String getSmallPicture(){
		for(ContentAttach ca : contentAttachs){
			if(ca.getAttachExt()==null)
				continue;
			if(ContentAttach.PICTURE_EXTS.contains(ca.getAttachExt().toLowerCase())){
				if("1".equals(ca.getAttachType()))
					return ca.getAttachUrl();
			}
		}
		return "";
	}
	
	/**附件中取中等尺寸图片*/
	@Transient
	public String getMiddlePicture(){
		for(ContentAttach ca : contentAttachs){
			if(ca.getAttachExt()==null)
				continue;
			if(ContentAttach.PICTURE_EXTS.contains(ca.getAttachExt().toLowerCase())){
				if("2".equals(ca.getAttachType()))
					return ca.getAttachUrl();
			}
		}
		return "";
	}
	/**附件中取大尺寸图片*/
	@Transient
	public String getBigPicture(){
		for(ContentAttach ca : contentAttachs){
			if(ca.getAttachExt()==null)
				continue;
			if(ContentAttach.PICTURE_EXTS.contains(ca.getAttachExt().toLowerCase())){
				if("3".equals(ca.getAttachType()))
					return ca.getAttachUrl();
			}
		}
		return "";
	}
	/**附件中取原图片*/
	@Transient
	public String getOrginPicture(){
		for(ContentAttach ca : contentAttachs){
			if(ca.getAttachExt()==null)
				continue;
			if(ContentAttach.PICTURE_EXTS.contains(ca.getAttachExt().toLowerCase())){
				if("0".equals(ca.getAttachType()))
					return ca.getAttachUrl();
			}
		}
		return "";
	}
	
	@Transient
	public String getSubContent(int index){
		if(StringUtils.isBlank(contentContext) || index<0) return null;
		String content =HtmlTextConverter.getPureTextFromHtml(contentContext);
		if(content.length() > index){
			return content.substring(0, index);
		}else{
			return content;
		}
	}
	
	/**此方法用于 全站动态查询时，依据文章id跳转到生成的静态页面，使用此方法必须预先赋值*/ 	 
	@Transient
	public String getVisiturl() {
		return visiturl;
	}

	public void setVisiturl(String visiturl) {
		this.visiturl = visiturl;
	}
	
	@Transient
	public String[] getKeyWordsArray(){
		if(StringUtils.isBlank(this.keywords))	return null;
		keywords = keywords.replaceAll("，", ",");
		return keywords.split(","); 
	}
	@Transient
	public String getProperties() {
		String showType = getShowType();
		int l = showType.length();
		String[] ps = new String[]{"原","顶","新","荐","精","热"};
		List<String> list = new ArrayList<String>();
		for(int i=0;i<l;i++){
			String s = String.valueOf(showType.charAt(i));
			if("1".equals(s)){
				list.add(ps[i]);
			}else if(i==0 && "0".equals(s)){
				list.add("转");
			}
			/*if(i==1 && "0".equals(s)){
				list.add(ps[i+1]);
			}
			if("1".equals(s)){
				if(i<2) list.add(ps[i]);
				if(i>1) list.add(ps[i+1]);
			}*/			
		}
		StringBuffer sb = new StringBuffer();		
		if(!list.isEmpty()){
			sb.append("[");
			for(String s:list){
				sb.append(s).append(" ");
			}
			sb.append("]");
		}
		list.clear(); 
		return sb.toString();
	} 
	 
}
